// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// A simple main() that uses the image_manipulator class

#include <cstdlib>
#include "compat.h"
#include "image_manipulator.h"
#include "image.h"
#include "image_utilities.h"

using namespace harmonizer;

DEFINE_string(input_filename, "", "Name of file we want to manipulate");
DEFINE_string(output_filename, "", "Name of output file");
DEFINE_string(segments, "", "Comma-separated list of segments (no spaces)");
DEFINE_int32(fit_method, 0,
             "0 - CLOSEST_VALID_VALUE, 1 - LINEAR_SCALE, 2 - GAUSSIAN_SCALE");

int main(int argc, char **argv) {
  google::ParseCommandLineFlags(&argc, &argv, true);

  CHECK(!FLAGS_input_filename.empty());
  CHECK(!FLAGS_output_filename.empty());

  // Convert FLAGS_segments to a vector of floats
  std::vector<float> seg_boundaries;
  vector<string> seg_boundaries_str = SplitString(FLAGS_segments, ",");
  for (string boundary : seg_boundaries_str) {
    float val = strtof(boundary.c_str(), NULL);
    seg_boundaries.push_back(val);
  }
  CHECK(seg_boundaries.size() > 0 && (seg_boundaries.size() % 2 == 0));

  ImageManipulator manip(FLAGS_input_filename);
  HueRange hue_range(seg_boundaries);

  switch (FLAGS_fit_method) {
    case 0:
      CHECK(manip.FitImageToHueRange(hue_range,
                                        HueRange::CLOSEST_VALID_VALUE));
      break;
    case 1:
      CHECK(manip.FitImageToHueRange(hue_range, HueRange::LINEAR_SCALE));
      break;
    case 2:
      CHECK(manip.FitImageToHueRange(hue_range, HueRange::GAUSSIAN_SCALE));
      break;
    case 3:
      CHECK(manip.FitImageToHueRange(hue_range,
                                        HueRange::LINEAR_SCALE_MINCUT));
      break;
    default:
      CHECK(false);
  }

  CHECK(manip.WriteImageToFile(FLAGS_output_filename));

  return 0;
}

